package com.cvv.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cvv.dao.FirstClassMapper;
import com.cvv.dao.ProductMapper;
import com.cvv.dao.SecondClassMapper;
import com.cvv.entity.FirstClass;
import com.cvv.entity.Product;
import com.cvv.entity.SecondClass;




@Controller
@RequestMapping("proapi")
public class ProductController {

	@Autowired
	private ProductMapper productmapper;
	
	@Autowired
	private FirstClassMapper firstmapper;
	
	@Autowired
	private SecondClassMapper secondmapper;
	
	@RequestMapping("getprobypage")
	public String getprobypage(Model model,HttpServletRequest request){
		String currentIndex = request.getParameter("pageIndex");
		String path = request.getParameter("path");
		if(path==null) {
			path = "../dist_hub/stock/select_stock.jsp";
		}
		int pageSize = 6;
		int pageIndex = 1;
		if(currentIndex!=null) {
			pageIndex=Integer.parseInt(currentIndex);
		}
		List<Product> listpro = productmapper.getprobypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = productmapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listpro", listpro);
		String p = "forward:"+path+"";
		return p;
	}
	
	@RequestMapping("getprobypage2")
	public String getprobypage2(Model model){
		String path = "../dist_hub/revert/select_revert.jsp";
		int pageSize = 6;
		int pageIndex = 1;
		List<Product> listpro = productmapper.getprobypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = productmapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listpro", listpro);
		String p = "forward:"+path+"";
		return p;
	}
	
	@RequestMapping("getprobypage3")
	public String getprobypage3(Model model){
		String path = "../dist_hub/goods/good_list.jsp";
		int pageSize = 6;
		int pageIndex = 1;
		List<Product> listpro = productmapper.getprobypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = productmapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listpro", listpro);
		String p = "forward:"+path+"";
		return p;
	}
	
	
	@RequestMapping("getprobyid")
	public String getprobyid(Model model,final int tex1,final String tex2,final int tex3) {
		Product pro = new Product(tex3,tex2,tex1);
		Product product = productmapper.getprobyid(tex3);
		model.addAttribute("product", product);
		model.addAttribute("num", tex1);
		model.addAttribute("date", tex2);
		return "forward:../dist_hub/stock/validate_stock.jsp";
	}
	
	@RequestMapping("getprobyid2")
	public String getprobyid2(Model model,final int tex1,final String tex2,final int tex3) {
		Product pro = new Product(tex3,tex2,tex1);
		Product product = productmapper.getprobyid(tex3);
		model.addAttribute("product", product);
		model.addAttribute("num", tex1);
		model.addAttribute("date", tex2);
		return "forward:../dist_hub/revert/validate_revert.jsp";
	}
	
	@RequestMapping("getprobyid3")
	public String getprobyid3(Model model,HttpServletRequest request) {
		int id = Integer.valueOf(request.getParameter("id"));
		Product product = productmapper.getprobyid(id);
		model.addAttribute("product", product);
		return "forward:../dist_hub/goods/goods.jsp";
	}
	
	@RequestMapping("getprobyid4")
	public String getprobyid4(Model model,final int tex1) {
		List<FirstClass> listfirst = firstmapper.listfirst();
		List<SecondClass> listsecond = secondmapper.listsecond();
		Product product = productmapper.getprobyid(tex1);
		model.addAttribute("listfirst", listfirst);
		model.addAttribute("listsecond", listsecond);
		model.addAttribute("product", product);
		return "forward:../dist_hub/goods/goods_update.jsp";
	}
	
	@RequestMapping("getprobyid5")
	public String getprobyid5(Model model,HttpServletRequest request) {
		List<FirstClass> listfirst = firstmapper.listfirst();
		List<SecondClass> listsecond = secondmapper.listsecond();
		int proid = Integer.valueOf(request.getParameter("proid"));
		Product product = productmapper.getprobyid(proid);
		model.addAttribute("listfirst", listfirst);
		model.addAttribute("listsecond", listsecond);
		model.addAttribute("product", product);
		return "forward:../dist_hub/goods/goods_update.jsp";
	}
	
	@RequestMapping("updateprobyid")
	public String updateprobyid(Model model,final int tex1,final String tex2,final int tex3) {
		Product pro = new Product(tex3,tex2,tex1);
		int num = productmapper.updateprobyid(pro);
		Product product = productmapper.getprobyid(tex3);
		model.addAttribute("product", product);
		model.addAttribute("num", tex1);
		return "forward:../dist_hub/stock/show_stock.jsp";
	}
	
	@RequestMapping("updateprobyid2")
	public String updateprobyid2(Model model,final int tex1,final String tex2,final int tex3) {
		Product pro = new Product(tex3,tex2,tex1);
		int num = productmapper.updateprobyid2(pro);
		Product product = productmapper.getprobyid(tex3);
		model.addAttribute("product", product);
		model.addAttribute("num", tex1);
		return "forward:../dist_hub/revert/show_revert.jsp";
	}
	
	@RequestMapping("updateprobyid3")
	public String updateprobyid3(Model model,final int tex1,final String tex2,final int tex3) {
		Product pro = new Product(tex3,tex2,tex1);
		int num = productmapper.updateprobyid2(pro);
		Product product = productmapper.getprobyid(tex3);
		model.addAttribute("product", product);
		model.addAttribute("num", tex1);
		return "forward:../dist_hub/revert/show_revert.jsp";
	}
	
	@RequestMapping("updateprobyid4")
	public String updateprobyid4(Model model,final int tex1,final String tex2,final String tex3,final int tex4,final int tex5,final int tex6,final String tex7,final String tex8,final String sel1,final String sel2) {
		Product product = new Product(tex1,tex2,sel2,tex3,tex4,tex5,tex7,sel1,tex8,tex6);
		int num = productmapper.updateprobyid4(product);
		return "redirect:/proapi/getprobypage3";
	}
	
	@RequestMapping("listall")
	public String listall(Model model) {
		List<FirstClass> listfirst = firstmapper.listfirst();
		List<SecondClass> listsecond = secondmapper.listsecond();
		List<Product> listpro = productmapper.getprofs();
		model.addAttribute("listfirst", listfirst);
		model.addAttribute("listsecond", listsecond);
		model.addAttribute("listpro", listpro);
		return "forward:../dist_hub/goods/goods_type_list.jsp";
	}
	
	@RequestMapping("addfirst")
	public String addfirst(Model model,final String tex1,final String tex2) {
		FirstClass fc = new FirstClass(tex1,tex2);
		int num = firstmapper.addfirstclass(fc);
		return "redirect:/proapi/listall";
	}
	
	@RequestMapping("addsecond")
	public String addsecond(Model model,final String tex3,final String tex4) {
		SecondClass sc = new SecondClass(tex3,tex4);
		int num = secondmapper.addsecondclass(sc);
		return "redirect:/proapi/listall";
	}
	
	@RequestMapping("delfirst")
	public String delfirst(Model model,HttpServletRequest request) {
		int id = Integer.valueOf(request.getParameter("firstid"));
		int num = firstmapper.delfirstclass(id);
		return "redirect:/proapi/listall";
	}
	
	@RequestMapping("addpro")
	public String addpro(Model model) {
		List<FirstClass> listfirst = firstmapper.listfirst();
		List<SecondClass> listsecond = secondmapper.listsecond();
		model.addAttribute("listfirst", listfirst);
		model.addAttribute("listsecond", listsecond);
		return "forward:../dist_hub/goods/goods_new.jsp";
	}
	
	@RequestMapping("addpro2")
	public String addpro2(Model model,final int tex1,final String tex2,final String tex3,final int tex4,final int tex5,final int tex6,final String tex8,final String tex10,final String tex11,final String sel1,final String sel2) {
		Product pro = new Product(tex1,tex2,sel2,tex3,tex4,tex5,tex8,tex11,sel1,tex10,tex6);
		int num = productmapper.addproduct(pro);
		return "redirect:/proapi/getprobypage3";
	}
	
	@RequestMapping("delprobyid")
	public String delprobyid(Model model,HttpServletRequest request) {
		int id = Integer.valueOf(request.getParameter("id"));
		int num = productmapper.delproduct(id);
		return "redirect:/proapi/getprobypage3";
	}
}
